package net.xdclass.config;

import lombok.Data;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Data
@Configuration
public class RabbitMQConfig {
    /**
     * 交换机
     */
    @Value("${mqconfig.stock_event_exchange}")
    private String eventExchange;


    /**
     * 第一个商品队列延迟队列，
     */
    @Value("${mqconfig.stock_release_delay_queue}")
    private String stockReleaseDelayQueue;

    /**
     * 第一个商品队列的路由key
     * 进入队列的路由key
     */
    @Value("${mqconfig.stock_release_delay_routing_key}")
    private String stockReleaseDelayRoutingKey;


    /**
     * 第二个商品队列，被监听恢复库存的队列
     */
    @Value("${mqconfig.stock_release_queue}")
    private String stockReleaseQueue;

    /**
     * 第二个商品队列的路由key
     *
     * 即进入死信队列的路由key
     */
    @Value("${mqconfig.stock_release_routing_key}")
    private String stockReleaseRoutingKey;

    /**
     * 第一个购物车队列延迟队列，
     */
    @Value("${mqconfig.stock_releasecart_delay_queue}")
    private String stockReleaseCartDelayQueue;

    /**
     * 第一个购物车队列的路由key
     * 进入队列的路由key
     */
    @Value("${mqconfig.stock_releasecart_delay_routing_key}")
    private String stockReleaseCartDelayRoutingKey;

    /**
     * 第二个购物车队列，被监听恢复库存的队列
     */
    @Value("${mqconfig.stock_releasecart_queue}")
    private String stockReleaseCartQueue;

    /**
     * 第二个商品队列的路由key
     *
     * 即进入死信队列的路由key
     */
    @Value("${mqconfig.stock_releasecart_routing_key}")
    private String stockReleaseCartRoutingKey;

    /**
     * 过期时间
     */
    @Value("${mqconfig.ttl}")
    private Integer ttl;

    /**
     * 消息转换器
     * @return
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }


    /**
     * 创建交换机 Topic类型，也可以用dirct路由
     * 一般一个微服务一个交换机
     * @return
     */
    @Bean
    public Exchange stockEventExchange(){
        return new TopicExchange(eventExchange,true,false);
    }


    /**
     * 商品延迟队列
     */
    @Bean
    public Queue stockReleaseDelayQueue(){

        Map<String,Object> args = new HashMap<>(3);
        args.put("x-message-ttl",ttl);
        args.put("x-dead-letter-routing-key",stockReleaseRoutingKey);
        args.put("x-dead-letter-exchange",eventExchange);

        return new Queue(stockReleaseDelayQueue,true,false,false,args);
    }

    /**
     * 购物车延迟队列
     */
    @Bean
    public Queue stockReleaseCartDelayQueue(){

        Map<String,Object> args = new HashMap<>(3);
        args.put("x-message-ttl",ttl);
        args.put("x-dead-letter-routing-key",stockReleaseCartRoutingKey);
        args.put("x-dead-letter-exchange",eventExchange);

        return new Queue(stockReleaseCartDelayQueue,true,false,false,args);
    }

    /**
     * 商品死信队列，普通队列，用于被监听
     */
    @Bean
    public Queue stockReleaseQueue(){

        return new Queue(stockReleaseQueue,true,false,false);

    }

    /**
     * 购物车死信队列，普通队列，用于被监听
     */
    @Bean
    public Queue stockReleaseCartQueue(){

        return new Queue(stockReleaseCartQueue,true,false,false);

    }


    /**
     * 第一个商品队列，即延迟队列的绑定关系建立
     * @return
     */
    @Bean
    public Binding stockReleaseDelayBinding(){

        return new Binding(stockReleaseDelayQueue,Binding.DestinationType.QUEUE,eventExchange,stockReleaseDelayRoutingKey,null);
    }

    /**
     * 第一个购物车队列，即延迟队列的绑定关系建立
     * @return
     */
    @Bean
    public Binding stockReleaseCartDelayBinding(){

        return new Binding(stockReleaseCartDelayQueue,Binding.DestinationType.QUEUE,eventExchange,stockReleaseCartDelayRoutingKey,null);
    }

    /**
     * 商品死信队列绑定关系建立
     * @return
     */
    @Bean
    public Binding stockReleaseBinding(){

        return new Binding(stockReleaseQueue,Binding.DestinationType.QUEUE,eventExchange,stockReleaseRoutingKey,null);
    }

    /**
     * 购物车死信队列绑定关系建立
     * @return
     */
    @Bean
    public Binding stockReleaseCartBinding(){

        return new Binding(stockReleaseCartQueue,Binding.DestinationType.QUEUE,eventExchange,stockReleaseCartRoutingKey,null);
    }

}
